﻿$(function () {
    $("#year").ligerComboBox({
        width: 60,
        value: (new Date).getFullYear(),
        valueFieldID: 'hdnYear',
    });
    $("#quarter").ligerComboBox({
        width: 80,
        cancelable: false,
        valueFieldID: 'hdnQuarter',
        data: [
              { id: 0, text: "全部" },
              { id: 1, text: "第一季度" },
              { id: 2, text: "第二季度" },
              { id: 3, text: "第三季度" },
              { id: 4, text: "第四季度" }
        ],
        onSelected: function (value) {
            var monthJson = [{ id: 0, text: "全部" }];
            switch (value) {
                case "1":
                    monthJson.push({ id: 1, text: "1" }, { id: 2, text: "2" }, { id: 3, text: "3" });
                    break;
                case "2":
                    monthJson.push({ id: 4, text: "4" }, { id: 5, text: "5" }, { id: 6, text: "6" });
                    break;
                case "3":
                    monthJson.push({ id: 7, text: "7" }, { id: 8, text: "8" }, { id: 9, text: "9" });
                    break;
                case "4":
                    monthJson.push({ id: 10, text: "10" }, { id: 11, text: "11" }, { id: 12, text: "12" });
                    break;
                default:
                    monthJson.push(
                                   { id: 1, text: "1" }, { id: 2, text: "2" }, { id: 3, text: "3" },
                                   { id: 4, text: "4" }, { id: 5, text: "5" }, { id: 6, text: "6" },
                                   { id: 7, text: "7" }, { id: 8, text: "8" }, { id: 9, text: "9" },
                                   { id: 10, text: "10" }, { id: 11, text: "11" }, { id: 12, text: "12" }
                                  );
            }
            $("#month").ligerComboBox({ width: 60, cancelable: false, valueFieldID: 'hdnMonth' }).setData(monthJson);
            $("#month").ligerComboBox().setValue("0")
        }
    });

    var month = new Date().getMonth();
    if (month < 3) {
        $("#quarter").ligerComboBox().setValue("1");
    } else if (month < 6) {
        $("#quarter").ligerComboBox().setValue("2");
    } else if (month < 9) {
        $("#quarter").ligerComboBox().setValue("3");
    } else if (month < 12) {
        $("#quarter").ligerComboBox().setValue("4");
    }

    $("#areaID").ligerComboBox({
        width: 100,
        cancelable: false,
        url: "/AdverseEvent/EditAdverseEvent/GetDepartmentList?ram" + Math.random(),
        parms: { isAll: 1 },
        valueField: "value",
        textField: "text",
        valueFieldID: 'hdnAreaID',
        onSuccess: function (data) {
            $("#areaID").ligerComboBox().setValue($("#hdnCurrDepCode").val());
            $('#btnSearch').click().focus();
        }
    });

    $('#btnSearch').click(function () {
        search();
    });

    $('#btnYcSum').click(function () {
        var year = $("#hdnYear").val();
        var quarter = $("#hdnQuarter").val();
        var month = $("#hdnMonth").val();
        var areaID = $("#hdnAreaID").val();
        location.href = "/AdverseEvent/AdverseEventSummary/YcSumIndex?year=" + year + "&quarter=" + quarter + "&month=" + month + "&areaID=" + areaID;
    });

    $("#btnYcReport").click(function () {
        var year = $("#hdnYear").val();
        var quarter = $("#hdnQuarter").val();
        var month = $("#hdnMonth").val();
        var areaID = $("#hdnAreaID").val();
        location.href = "/AdverseEvent/AdverseEventSummary/YcReport?year=" + year + "&quarter=" + quarter + "&month=" + month + "&areaID=" + areaID;
    });
});

//加载列表
function initTable(queryData) {
    var data;

    $.ajax({
        url: "/AdverseEvent/AdverseEventSummary/Search?ram" + Math.random(),
        type: "post",
        async: false,
        data: queryData,
        success: function (r) {
            data = $.parseJSON(r);
        }
    });

    var rows = data.Rows;
    var columns = [];
    var eventType = data.EventType;
    columns.push({ display: '月份/类型', name: 'Month' });
    for (var i = 0; i < eventType.length; i++) {
        columns.push({ display: eventType[i].Name, name: 'EventType' + eventType[i].ID });
    }
    columns.push({ display: '总计', name: 'Total' });
    $("#divMaingrid").html('<div id="maingrid"></div>');
    $("#maingrid").ligerGrid({
        data: { Rows: rows },
        usePager: false,
        rownumbers: true,
        columns: columns
    });
    GridNoData($("#maingrid"));
    $(".l-grid").height($(".l-grid").height() - 1);

    var legendData = [];
    var seriesData = [];
    for (var i = 0; i < eventType.length; i++) {
        legendData.push(eventType[i].Name);
        seriesData.push({ value: rows[rows.length - 1]["EventType" + eventType[i].ID], name: eventType[i].Name });
    }

    var myChart = echarts.init(document.getElementById('mainecharts'));
    option = {
        title: {
            text: '不良事件分类占比情况',
            subtext: '',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            x: 'left',
            data: legendData
        },
        toolbox: {
            show: true,
            feature: {
                mark: { show: true },
                dataView: { show: false, readOnly: false, width: '50%' },
                magicType: {
                    show: true,
                    type: ['pie', 'funnel'],
                    option: {
                        funnel: {
                            x: '25%',
                            width: '50%',
                            funnelAlign: 'left',
                            max: 500
                        }
                    }
                },
                restore: { show: true },
                saveAsImage: { show: true }
            }
        },
        calculable: true,
        series: [
            {
                name: '事件类型',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: seriesData
            }
        ]
    };
    myChart.setOption(option);

}

//搜索
function search() {
    var queryData = {
        year: $("#hdnYear").val(),
        quarter: $("#hdnQuarter").val(),
        month: $("#hdnMonth").val(),
        areaID: $("#hdnAreaID").val()
    }
    initTable(queryData);
}

